package com.dituhui.gps.vo;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.dituhui.tool.gis.Line;
import com.dituhui.tool.gis.Point;
import com.dituhui.tool.util.DateUtil;
import com.dituhui.tool.util.StringUtil;

/**
 * GPS线
 * 
 * @author liweigu
 * 
 */
public class GPSLine extends Line {
	private static final Logger LOGGER = Logger.getLogger(GPSLine.class);
	private List<GPSPoint> points = new ArrayList<GPSPoint>();
	// 跟其他GPSPoint的GPSTime发生重复的
	private static String DeleteTag_RepeatedGPSTime = "1001";
	// GPSTime不是当天的
	private static String DeleteTag_InvalidGPSTime = "1002";

	/**
	 * 获取点列表
	 * 
	 * @return 点列表
	 */
	public List<GPSPoint> getGPSPoints() {
		return this.points;
	}

	/**
	 * 设置点列表
	 * 
	 * @param points 点列表
	 */
	public void setGPSPoints(List<GPSPoint> points) {
		this.points = points;
	}

	@Override
	/**
	 * 获取点列表
	 * 
	 * @return 点列表
	 */
	public List<Point> getPoints() {
		List<Point> points = null;
		if (this.points != null) {
			points = new ArrayList<Point>();
			for (GPSPoint gpsPoint : this.points) {
				points.add((Point) gpsPoint);
			}
		}
		return points;
	}

	@Override
	/**
	 * 设置点列表
	 * 
	 * @param points 点列表
	 */
	public void setPoints(List<Point> points) {
		List<GPSPoint> gpsPoints = null;
		if (points != null) {
			gpsPoints = new ArrayList<GPSPoint>();
			for (Point point : points) {
				points.add((GPSPoint) point);
			}
		}
		this.points = gpsPoints;
	}

	public String toCSVString() {
		StringBuilder result = new StringBuilder();
		int i = 0;
		for (GPSPoint gpsPoint : this.points) {
			if (gpsPoint == null) {
				continue;
			}
			if (i++ > 0) {
				result.append(StringUtil.DoubleUnderline);
			}
			result.append(gpsPoint.toCSVString());
		}
		return result.toString();
	}

	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		for (GPSPoint gpsPoint : this.points) {
			result.append(gpsPoint);
		}
		return result.toString();
	}

	/**
	 * 去重、去除非当天坐标，找出的打上DeleteTag分别为1001和1002
	 * 
	 * @param gpsPoints 点串。点串里对象的值会发生变化。
	 * @param comparedDay 日期
	 */
	public static void distinctAndFilterByGPSTimeToDeleteTag(List<GPSPoint> gpsPoints, String comparedDay) {
		Set<String> gpsTimeSet = new HashSet<String>();
		DateFormat gpsTimeFormat = DateUtil.getGPSTimeFormat();
		DateFormat dayDateFormat = DateUtil.getDayFormat();
		if (comparedDay == null) {
			// 以当天作为比较对象
			comparedDay = dayDateFormat.format(new Date());
		}
		if (gpsPoints != null && gpsPoints.size() > 0) {
			for (int i = 0; i < gpsPoints.size(); i++) {
				GPSPoint gpsPoint = gpsPoints.get(i);
				if (gpsPoint == null || (gpsPoint.getDeleteTag() != null)) {
					// “gpsPoint为空”以及“标记为已删除”的不处理
				} else if (gpsPoint.getGPSTime() == null) {
					// GPSTime为空的，设置删除标记
					gpsPoint.setDeleteTag(DeleteTag_InvalidGPSTime);
				} else {
					String gpsTime = gpsTimeFormat.format(gpsPoint.getGPSTime());
					if (gpsTimeSet.contains(gpsTime)) {
						// gpsTime重复的，设置删除标记
						gpsPoint.setDeleteTag(DeleteTag_RepeatedGPSTime);
					} else {
						String gpsDay = dayDateFormat.format(gpsPoint.getGPSTime());
						if (!comparedDay.equals(gpsDay)) {
							// GPSTime不是当天的，设置删除标记
							gpsPoint.setDeleteTag(DeleteTag_InvalidGPSTime);
						} else {
							gpsTimeSet.add(gpsTime);
						}
					}
				}
			}
		}
	}

	/**
	 * 获按时间排序。会去除null成员。
	 * 
	 * @param originalList 待排序坐标
	 */
	public static boolean sortByTime(List<GPSPoint> originalList) {
		boolean flag = false;

		boolean hasNullGPSTime = false;
		for (int i = originalList.size() - 1; i >= 0; i--) {
			GPSPoint gpsPoint = originalList.get(i);
			if (gpsPoint == null) {
				originalList.remove(i);
			} else if (gpsPoint.getGPSTime() == null) {
				hasNullGPSTime = true;
				LOGGER.warn("hasNullGPSTime. gpsPoint=" + gpsPoint);
			}
		}
		if (!hasNullGPSTime) {
			// 按时间进行排序
			Collections.sort(originalList, new Comparator<GPSPoint>() {
				public int compare(GPSPoint arg0, GPSPoint arg1) {
					// 如果GPSTime为空会发生异常
					return arg0.getGPSTime().compareTo(arg1.getGPSTime());
				}
			});
			// 完成排序后设置flag为true
			flag = true;
		} else {
			// GPSTime有空值值不排序
		}

		return flag;
	}

	/**
	 * 根据删除标记筛选
	 * 
	 * @param gpsPoints GPSPoint列表
	 * @param deleted 是否删除的
	 * @return GPSPoint列表
	 */
	public static List<GPSPoint> filterByDeleteTag(List<GPSPoint> gpsPoints, boolean deleted) {
		List<GPSPoint> gpsPointsResult = new ArrayList<GPSPoint>();

		if (gpsPoints != null) {
			for (GPSPoint gpsPoint : gpsPoints) {
				if (deleted) {
					// 筛选已删除的
					if (gpsPoint.getDeleteTag() != null && gpsPoint.getDeleteTag().length() > 0 && !gpsPoint.getDeleteTag().contains(StringUtil.NULL)) {
						// System.out.println("deleted point:" + gpsPoint.toCSVString());
						gpsPointsResult.add(gpsPoint);
					}
				} else {
					// 筛选未删除的
					if (gpsPoint.getDeleteTag() == null || gpsPoint.getDeleteTag().length() == 0 || gpsPoint.getDeleteTag().contains(StringUtil.NULL)) {
						gpsPointsResult.add(gpsPoint);
					}
				}
			}
		}

		return gpsPointsResult;
	}

	@Override
	public Object clone() {
		Object copiedGPSPoints = null;
		if (this.points != null) {
			copiedGPSPoints = new ArrayList<GPSPoint>();
			for (GPSPoint gpsPoint : this.points) {
				GPSPoint copiedGPSPoint = new GPSPoint(gpsPoint);
				((List<GPSPoint>) copiedGPSPoints).add(copiedGPSPoint);
			}
		}
		return copiedGPSPoints;
	}
}
